/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolóxico de Galicia
 * Copyright (C) 2010-2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.costcategories;

import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.costcategories.entities.TypeOfWorkHours;
import org.libreplan.web.common.BaseCRUDController;
import org.libreplan.web.common.Level;
import org.libreplan.web.common.Util;
import org.libreplan.web.common.components.NewDataSortableGrid;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.CheckEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Messagebox;

import java.util.ConcurrentModificationException;
import java.util.List;

import static org.libreplan.web.I18nHelper._;

/**
 * Controller for CRUD actions over a {@link TypeOfWorkHours}
 *
 * @author Jacobo Aragunde Perez <jaragunde@igalia.com>
 * @author Cristina Alvarino Pereza <cristina.alvarino@comtecsf.es>
 */
@SuppressWarnings("serial")
public class TypeOfWorkHoursCRUDController extends BaseCRUDController<TypeOfWorkHours> {

    private ITypeOfWorkHoursModel typeOfWorkHoursModel;

    private NewDataSortableGrid listing;

    public TypeOfWorkHoursCRUDController(){
        typeOfWorkHoursModel = (ITypeOfWorkHoursModel) SpringUtil.getBean("typeOfWorkHoursModel");
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        listing = (NewDataSortableGrid) listWindow.getFellowIfAny("listing");
    }

    protected void save() throws ValidationException {
        typeOfWorkHoursModel.confirmSave();
    }

    public List<TypeOfWorkHours> getTypesOfWorkHours() {
        return typeOfWorkHoursModel.getTypesOfWorkHours();
    }

    public TypeOfWorkHours getTypeOfWorkHours() {
        return typeOfWorkHoursModel.getTypeOfWorkHours();
    }

    public void onCheckGenerateCode(Event e) {
        CheckEvent ce = (CheckEvent) e;
        if (ce.isChecked()) {
            try {
                // We have to auto-generate the code
                typeOfWorkHoursModel.setCodeAutogenerated(ce.isChecked());
            } catch (ConcurrentModificationException err) {
                messagesForUser.showMessage(Level.ERROR, err.getMessage());
            }
        }
        Util.reloadBindings(editWindow);
    }

    private boolean isReferencedByOtherEntities(TypeOfWorkHours typeOfWorkHours) {
        try {
            typeOfWorkHoursModel.checkIsReferencedByOtherEntities(typeOfWorkHours);
            return false;
        } catch (ValidationException e) {
            showCannotDeleteWorkHoursTypeDialog(e.getInvalidValue().getMessage());
        }
        return true;
    }

    private void showCannotDeleteWorkHoursTypeDialog(String message) {
        Messagebox.show(_(message), _("Warning"), Messagebox.OK, Messagebox.EXCLAMATION);
    }

    @Override
    protected String getEntityType() {
        return _("Hours Type");
    }

    @Override
    protected String getPluralEntityType() {
        return _("Hours Types");
    }

    @Override
    protected void initCreate() {
        typeOfWorkHoursModel.initCreate();
    }

    @Override
    protected void initEdit(TypeOfWorkHours typeOfWorkHours) {
        typeOfWorkHoursModel.initEdit(typeOfWorkHours);
    }

    @Override
    protected TypeOfWorkHours getEntityBeingEdited() {
        return typeOfWorkHoursModel.getTypeOfWorkHours();
    }

    @Override
    protected void delete(TypeOfWorkHours typeOfWorkHours) throws InstanceNotFoundException {
        typeOfWorkHoursModel.confirmRemove(typeOfWorkHours);
    }

    protected boolean beforeDeleting(TypeOfWorkHours typeOfWorkHours) {
        return !isReferencedByOtherEntities(typeOfWorkHours);
    }

    public String getCurrencySymbol() {
        return Util.getCurrencySymbol();
    }

    public String getMoneyFormat() {
        return Util.getMoneyFormat();
    }

}
